package com.ruoyi.web.controller.dictionary;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.SysMatClassificationType;
import com.ruoyi.domain.SysMatType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.ISysMatClassificationTypeService;
import com.ruoyi.service.ISysMatTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 材料分类Controller
 *
 * @author ruoyi
 * @date 2020-07-03
 */
@Controller
@RequestMapping("/ruoyi/classification")
public class SysMatClassificationTypeController extends BaseController {
    private String prefix = "ruoyi/classification";

    @Autowired
    private ISysMatClassificationTypeService sysMatClassificationTypeService;
    @Autowired
    private ISysMatTypeService sysMatTypeService;

    @RequiresPermissions("ruoyi:classification:view")
    @GetMapping()
    public String type() {
        return prefix + "/type";
    }


    /**
     * 查询材料分类列表
     */
    @RequiresPermissions("ruoyi:classification:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(String matType) {
        startPage();
        List<SysMatClassificationType> list = sysMatClassificationTypeService.selectSysMatClassificationTypeLists(matType);
        return getDataTable(list);
    }

    /**
     * 导出材料分类列表
     */
    @RequiresPermissions("ruoyi:classification:export")
    @Log(title = "材料分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysMatClassificationType sysMatClassificationType) {
        List<SysMatClassificationType> list = sysMatClassificationTypeService.selectSysMatClassificationTypeList(sysMatClassificationType);
        ExcelUtil<SysMatClassificationType> util = new ExcelUtil<SysMatClassificationType>(SysMatClassificationType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 新增材料分类
     */
    @GetMapping("/add/{matType}")
    public String add(@PathVariable("matType")String matType, ModelMap mmap) {
        mmap.put("matType", matType);
        return prefix + "/add";
    }

    /**
     * 新增保存材料分类
     */
    @RequiresPermissions("ruoyi:classification:add")
    @Log(title = "材料分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysMatClassificationType sysMatClassificationType) {
        //获取创建者
        sysMatClassificationType.setMatClassificationCreateBy(ShiroUtils.getLoginName());
        //获取创建时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
        return toAjax(sysMatClassificationTypeService.insertSysMatClassificationType(sysMatClassificationType));
    }

    /**
     * 修改材料分类
     */
    @GetMapping("/edit/{matClassificationId}")
    public String edit(@PathVariable("matClassificationId") Integer matClassificationId, ModelMap mmap) {
        SysMatClassificationType sysMatClassificationType = sysMatClassificationTypeService.selectSysMatClassificationTypeById(matClassificationId);
        mmap.put("sysMatClassificationType", sysMatClassificationType);
        return prefix + "/edit";
    }

    /**
     * 修改保存材料分类
     */
    @RequiresPermissions("ruoyi:classification:edit")
    @Log(title = "材料分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysMatClassificationType sysMatClassificationType) {
        //获取创建者
        sysMatClassificationType.setMatClassificationCreateBy(ShiroUtils.getLoginName());
        //获取创建时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
        return toAjax(sysMatClassificationTypeService.updateSysMatClassificationType(sysMatClassificationType));
    }

    /**
     * 删除材料分类
     */
    @RequiresPermissions("ruoyi:classification:remove")
    @Log(title = "材料分类", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sysMatClassificationTypeService.deleteSysMatClassificationTypeByIds(ids));
    }

    /**
     * 查询字典详细
     */
    @RequiresPermissions("ruoyi:classification:details")
    @GetMapping("/details/{matType}")
    public String detail(@PathVariable("matType") String matType, ModelMap mmap) {
        mmap.put("matType",matType);
        return prefix + "/type";
    }
}
